package com.tanhua.admin.controller;

import com.tanhua.admin.service.UsersService;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.dto.FreezeDto;
import com.tanhua.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Xiao
 * @version 1.0
 * @description 用户管理接口
 * @date 2021/11/3
 */
@RestController
@RequestMapping("/manage/users")
public class UsersController {
    @Autowired
    private UsersService usersService;

    /**
     * 用户管理-用户基本资料
     * @param userID    用户id
     * @return
     */
    @GetMapping("/{userID}")
    public ResponseEntity getProfile(@PathVariable Long userID) {
        UserInfo userInfo = usersService.getProfile(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 用户管理-用户数据翻页
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity getProfiles(@RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = usersService.getProfiles(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 用户管理-冻结用户
     * @param freezeDto  userId、freezingTime、freezingRange、reasonsForFreezing、frozenRemarks
     * @return
     */
    @PostMapping("/freeze")
    public ResponseEntity freeze(@RequestBody FreezeDto freezeDto) {
        String userStatus = "0";
        String message = usersService.freeze(freezeDto, userStatus);
        Map<String, String> map = new HashMap<>();
        map.put("message", message);
        return ResponseEntity.ok(map);
    }

    /**
     * 用户管理-解冻用户
     * @param freezeDto  userId、reasonsForThawing
     * @return
     */
    @PostMapping("/unfreeze")
    public ResponseEntity unfreeze(@RequestBody FreezeDto freezeDto) {
        String userStatus = "1";
        String message = usersService.freeze(freezeDto, userStatus);
        Map<String, String> map = new HashMap<>();
        map.put("message", message);
        return ResponseEntity.ok(map);
    }
}
